package com.qfedu.edu.iotserver.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qfedu.edu.iotserver.service.ExceptionBusinessService;
import com.qfedu.edu.pojo.ExceptionBusiness;
import com.qfedu.edu.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaobobo
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/iotserver/exception-business")
@Api(tags = "异常业务的处理模块")
public class ExceptionBusinessController {


    @Autowired
    private ExceptionBusinessService exceptionBusinessService;

    /**
     * 分页查询所有的异常业务便于手动重试
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{currentPage}/{pageSize}")
    @ApiOperation("分页查询所有的异常业务")
    public R list(@PathVariable("currentPage") Integer currentPage,
                  @PathVariable("pageSize") Integer pageSize){
        IPage<ExceptionBusiness> page=exceptionBusinessService.findAll(currentPage,pageSize);
        return R.ok().data("total",page.getTotal()).data("list",page.getRecords());
    }


    /**
     * 业务重试
     * @param id
     * @return
     */
    @GetMapping("/businessRetry/{id}")
    @ApiOperation("业务重试")
    public R businessRetry(@PathVariable("id") String id) throws UnsupportedEncodingException {
        exceptionBusinessService.retry(id);
        return R.ok();
    }

    /**
     * 通过mqttId查询消息的状态
     * @param mqttId
     * @return
     */
    @GetMapping("/findQueueMessageRetryStatus/{mqttId}")
    public R findQueueMessageRetryStatus(@PathVariable("mqttId") String mqttId){
        Integer status=exceptionBusinessService.findQueueMessageRetryStatus(mqttId);
        return R.ok().data("status",status);
    }

}

